package com.gxwgy.wg2404.controller;

import com.gxwgy.wg2404.pojo.EventsPojo;
import com.gxwgy.wg2404.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 姚相国
 * @version 1.0
 * @className s
 * @date 2024/12/18 02:56
 */
@WebServlet({"/organizer/editEvent"})
public class OrganizerServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String eventId = request.getParameter("eventId");

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM events WHERE id = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, eventId);
            rs = ps.executeQuery();

            if (rs.next()) {
                EventsPojo event = new EventsPojo();
                event.setId(rs.getString("id"));
                event.setName(rs.getString("name"));
                event.setDescription(rs.getString("description"));
                event.setStart_time(rs.getString("start_time"));
                event.setEnd_time(rs.getString("end_time"));
                event.setLocation(rs.getString("location"));
                event.setCapacity(rs.getInt("capacity"));
                event.setCurrent_enrollment(rs.getInt("current_enrollment"));
                event.setStatus(rs.getString("status"));

                request.setAttribute("event", event);
                request.getRequestDispatcher("/editEvent.jsp").forward(request, response);
            } else {
                response.sendRedirect(request.getContextPath() + "/error.jsp");
            }
        } catch (SQLException e) {
            throw new ServletException(e);
        } finally {
            DBUtil.close(conn, ps, rs);
        }
    }
}

